package com.leo.train.business.service.Impl;

import com.leo.train.business.service.EnumService;
import com.leo.train.common.enums.ConfirmOrderStatusEnum;
import com.leo.train.common.enums.SeatColEnum;
import com.leo.train.common.enums.SeatTypeEnum;
import com.leo.train.common.enums.TrainTypeEnum;
import com.leo.train.common.resp.EnumInfoDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class EnumServiceImpl implements EnumService {
    @Autowired
    private RedisTemplate redisTemplate;

    // 定义枚举key的前缀
    private static final String ENUM_KEY = "enum:";
    // 定义过期时间 24小时
    private static final long CACHE_EXPIRE_TIME = 24 * 60 * 60;

    /**
     * 获取枚举列表
     * @param code 枚举code
     * @return
     */
    @Override
    public List<EnumInfoDto> getEnumListByCode(String code) {
        String cacheKey = ENUM_KEY + code;
        // 先从缓存中读取
        List<EnumInfoDto> cacheData = (List<EnumInfoDto>) redisTemplate.opsForValue().get(cacheKey);
        // 如果缓存中存在数据，则返回缓存数据
        if(!CollectionUtils.isEmpty(cacheData)) {
            return cacheData;
        }
        // 如果缓存中不存在数据，则从枚举中查询
        List<EnumInfoDto> result = getEnumListFromEnum(code);
        // 存入缓存
        if (!CollectionUtils.isEmpty(result)) {
            redisTemplate.opsForValue().set(cacheKey, result, CACHE_EXPIRE_TIME, TimeUnit.SECONDS);
            log.info("枚举数据存入缓存: {}", code);
        }
        return result;
    }

    /**
     * 从枚举中获取枚举列表
     * @param code 枚举code
     * @return
     */
    private List<EnumInfoDto> getEnumListFromEnum(String code) {
        List<EnumInfoDto> result = new java.util.ArrayList<>();

        switch (code) {
            case "train_type":
                for (TrainTypeEnum type : TrainTypeEnum.values()) {
                    EnumInfoDto sd = new EnumInfoDto();
                    sd.setCode(type.getCode());
                    sd.setDescription(type.getDescription());
                    result.add(sd);
                }
                break;
            case "seat_type":
                for (SeatTypeEnum type : SeatTypeEnum.values()) {
                    EnumInfoDto sd =new EnumInfoDto();
                    sd.setCode(type.getCode());
                    sd.setDescription(type.getDesc());
                    sd.setPrice(type.getPrice());
                    result.add(sd);
                }
                break;
            case "seat_col":
                for (SeatColEnum type : SeatColEnum.values()) {
                    EnumInfoDto sd = new EnumInfoDto();
                    sd.setCode(type.getCode());
                    sd.setDescription(type.getDesc());
                    sd.setType(type.getType());
                    result.add(sd);
                }
                break;
            case "confirm_order_status":
                for (ConfirmOrderStatusEnum type : ConfirmOrderStatusEnum.values()) {
                    EnumInfoDto sd = new EnumInfoDto();
                    sd.setCode(type.getCode());
                    sd.setDescription(type.getDesc());
                    result.add(sd);
                }
                break;
            default:
                log.warn("未知的枚举类型: {}", code);
                break;
        }

        return result;
    }

    /**
     * 清除枚举缓存
     * @param code 枚举code
     */
    public void clearEnumCache(String code) {
        String cacheKey = ENUM_KEY + code;
        redisTemplate.delete(cacheKey);
        log.info("清除枚举缓存: {}", code);
    }
}
